$(document).ready(function () {
    // 注册功能
    // 点击发送验证码---------------------------------
    $("#getYzm").click(async function () {
        var UserTelCon = $("#regUserTel").val();
        if ((/^1[3456789]\d{9}$/.test(UserTelCon))) {
            var sendData = `mobile=${UserTelCon}&type=2`; //默认给type为2因为这个教学平台，默认为老师
            let res = await sendAjax("get", `${testUrl}/api/sms/send`, sendData);
            // console.log(res);
            alert(res.msg);
        } else {
            alert("手机号码有误，请重填");
            return false;
        }
    })
    $("#regBtn").click(async function () {
        let regYzm = $("#regYzm").val();
        let regUserTel = $("#regUserTel").val();
        let regUserPwd = $("#regUserPwd").val();
        let regUserRePwd = $("#regUserRePwd").val();
        let regUserName = $("#regUserName").val();
        let data = {
            nickname: regUserName,
            code: regYzm,
            password: regUserPwd,
            repassword: regUserRePwd,
            mobile: regUserTel
        }
        // console.log(data);
        let res = await sendAjax("post", "" + testUrl + "/platform/user/register", data);
        alert(res.msg);
        if (res.msg === '注册成功') {
            $(".loginDialog .selTitle div").css("display", "none");
            $(".loginDialog .selTitle .login").css("display", "block");
            $(".loginDialog .selTitle #telZC").addClass("active");
            $(".loginDialog .selTitle #wxZC").removeClass("active");
            $(".loginDialog .loginMeth div.common").css("display", "none");
            $(".loginDialog .loginMeth .telLogin").css("display", "block");
        }
    })
    //  登录功能(手机号+验证码)---------------------------------
    $("#logGetYzm").click(async function (e) {
        e.preventDefault();
        let UserTelCon = $("#logUserTel").val();
        var sendData = `mobile=${UserTelCon}&type=2&event=mobilelogin`;
        // let res = await sendAjax("get", "http://39.98.43.82/api/sms/send", sendData);
        let res = await sendAjax("get", `${testUrl}/api/sms/send`, sendData);
        alert(res.msg)
        // console.log(res);
    });
    $("#logTel").click(async function () {
        let logUserTel = $("#logUserTel").val();
        let logYzm = $("#logYzm").val();
        console.log(logUserTel, logYzm);
        let data = {
            mobile: logUserTel,
            captcha: logYzm,
        };
        let res = await sendAjax("get", "" + testUrl + "/platform/user/mobile_login", data);
        console.log(res);
        if (res.msg === "登录成功") {
            // let userinfo = localStorage.setItem('userinfo', JSON.stringify(res.data.userinfo));
            // 存储时间为3个小时
            // delete res.data.userinfo.mobile;   //删除掉登录信息里的mobile手机号字段
            let userinfo = extendLocalStorage.set("userinfo", res.data.userinfo, new Date().getTime() + 86400000);;
            token = res.data.userinfo.token;
            alert(res.msg);
            // $(".loginDialog").css("display", "none");
            location.href = "index.html";
        } else {
            alert(res.msg);
        }
        // console.log(res);
        // 登录后关闭登录按钮，跳转首页
    })
    // 重置密码（登录之前的行为）-----------------------------------------
    $("#sendRePwdYzm").click(async function () {
        var UserTelCon = $("#rePwdTel").val();
        if ((/^1[3456789]\d{9}$/.test(UserTelCon))) {
            var sendData = `mobile=${UserTelCon}&type=2&event=remake`;
            //默认给type为2因为这个教学平台，默认为老师。event默认为remake,否则已注册的不能发送
            let res = await sendAjax("get", "http://39.98.43.82/api/sms/send", sendData);
            // console.log(res);
            alert(res.msg);
        } else {
            alert("手机号码有误，请重填");
            return false;
        }
    })
    $("#rePwdReg").click(async function () {
        let rePwdTel = $("#rePwdTel").val();
        let rePwdYzm = $("#rePwdYzm").val();
        let rePwdUserPwd = $("#rePwdUserPwd").val();
        let rePwdUserRePwd = $("#rePwdUserRePwd").val();
        // let userinfo=JSON.parse(localStorage.getItem("userinfo"));
        let data = {
            mobile: rePwdTel,
            password: rePwdUserPwd,
            repassword: rePwdUserRePwd,
            code: rePwdYzm,
            // token:userinfo.token
        };
        // console.log(data);
        let res = await sendAjax("POST", "" + testUrl + "/platform/user/remake", data);
        // console.log(res);
        alert(res.msg)

        if (res.msg === "确定") {
            // 成功之后跳转登录
            $(".loginDialog .selTitle div").css("display", "none");
            $(".loginDialog .selTitle .login").css("display", "block");
            $(".loginDialog .selTitle #telZC").addClass("active");
            $(".loginDialog .selTitle #wxZC").removeClass("active");
            $(".loginDialog .loginMeth div.common").css("display", "none");
            $(".loginDialog .loginMeth .telLogin").css("display", "block");
            alert("修改成功跳转登录页面");
        } else {
            alert(res.msg)
        }
    })
    // 微信与手机绑定
    $("#bindTelYzm").click(async function () {
        var UserTelCon = $("#bindUserTel").val();
        if ((/^1[3456789]\d{9}$/.test(UserTelCon))) {
            var sendData = `mobile=${UserTelCon}&type=2&event=remake`;
            //默认给type为2因为这个教学平台，默认为老师。event默认为remake,否则已注册的不能发送
            let res = await sendAjax("get", "http://39.98.43.82/api/sms/send", sendData);
            console.log(res);
            alert(res.msg);
        } else {
            alert("手机号码有误，请重填");
            return false;
        }
    })

    // 点击协议进去协议页面
    $(".localUserAgree").click(function () {
        window.open("userAgreement.html");
    })

    //  登录功能(手机号+密码)---------------------------------
    // $("#logTel").click(async function () {
    //     let logUserTel = $("#logUserTel").val();
    //     let logUserPwd = $("#logUserPwd").val();
    //     let data = {
    //         mobile: logUserTel,
    //         password: logUserPwd,
    //     };
    //     let res = await sendAjax("POST", "" + testUrl + "/platform/user/login", data);
    //     if (res.msg === "登录成功") {
    //         //是否选中记住密码
    //         let remPwd = $(".loginDialog .loginMeth .pwdOpera input[type=checkbox]").prop("checked");
    //         if (remPwd) {
    //             // 如果选中记住密码，则将账号密码存进cookies中，并且保存时间为7天   
    //             Cookies.set('remPwdUserinfo', window.btoa(JSON.stringify(data)), {
    //                 expires: 7,
    //                 path: "/"
    //             }); //只能放到线上才可以显示
    //         }
    //         let userinfo = localStorage.setItem('userinfo', JSON.stringify(res.data.userinfo));
    //         token = res.data.userinfo.token;
    //         alert(res.msg);
    //         $(".loginDialog").css("display", "none");
    //         location.href = "myCourse.html";
    //     } else {
    //         alert(res.msg);
    //     }
    //     // console.log(res);
    //     // 登录后关闭登录按钮，跳转首页
    // })
});